package com.tobeface.wechat.message.builder;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tobeface.modules.lang.Function;
import com.tobeface.modules.lang.Langs;
import com.tobeface.modules.lang.Requires;
import com.tobeface.wechat.message.WeChatMessage;
import com.tobeface.wechat.message.WeChatMessageBuilder;
import com.tobeface.wechat.message.WeChatMessagePayloads;
import com.tobeface.wechat.message.WeChatMessageTypes;

import java.util.List;
import java.util.Map;

/**
 * @author loudyn
 */
public class NewsTypedMessageBuilder extends AbstractTypedMessageBuilder {

    private final List<OutNewsItemMessageBuilder> items = Lists.newArrayList();

    /**
     * @param parent
     */
    public NewsTypedMessageBuilder(WeChatMessageBuilder parent) {
        super(parent);
    }

    /**
     * @return
     */
    public OutNewsItemMessageBuilder item() {
        OutNewsItemMessageBuilder item = new OutNewsItemMessageBuilder(this);
        items.add(item);
        return item;
    }


    @Override
    protected void configPayloads(Map<String, Object> payloads) {
        payloads.put(WeChatMessagePayloads.MSGTYPE_PAYLOAD, WeChatMessageTypes.NEWS);
        payloads.put("ArticleCount", items.size());
        payloads.put("Artices", Langs.map(items, new Function<OutNewsItemMessageBuilder, Object>() {
            @Override
            public Object apply(OutNewsItemMessageBuilder itemBuilder) {
                return itemBuilder.props;
            }
        }));
    }

    /**
     *
     */
    public class OutNewsItemMessageBuilder {
        final NewsTypedMessageBuilder parent;
        final Map<String, String> props = Maps.newHashMap();

        /**
         * @param parent
         */
        OutNewsItemMessageBuilder(NewsTypedMessageBuilder parent) {
            this.parent = parent;
        }

        /**
         * @param title
         * @return
         */
        public OutNewsItemMessageBuilder title(String title) {
            Requires.hasText(title, "title must not be null or empty!");
            props.put(WeChatMessagePayloads.TITLE_PAYLOAD, title);
            return this;
        }

        /**
         * @param descr
         * @return
         */
        public OutNewsItemMessageBuilder descr(String descr) {
            Requires.hasText(descr, "descr must not be null or empty!");
            props.put(WeChatMessagePayloads.DESCR_PAYLOAD, descr);
            return this;
        }

        /**
         * @param picUrl
         * @return
         */
        public OutNewsItemMessageBuilder picUrl(String picUrl) {
            Requires.hasText(picUrl, "picUrl must not be null!");
            props.put(WeChatMessagePayloads.PICURL_PAYLOAD, picUrl);
            return this;
        }

        /**
         * @param url
         * @return
         */
        public OutNewsItemMessageBuilder url(String url) {
            Requires.hasText(url, "url must not be null or empty!");
            props.put(WeChatMessagePayloads.URL_PAYLOAD, url);
            return this;
        }

        public OutNewsItemMessageBuilder item() {
            return parent.item();
        }

        /**
         * @return
         */
        public WeChatMessage build() {
            return parent.build();
        }
    }
}
